package com.xiaoxu;

import java.util.ArrayList;
import java.util.Scanner;

public class BankAccount {
    //银行账户类
    String Id;//账号
    String password;//密码
    double money;//存款余额
    String phoneNumber;//联系电话
    String name;//姓名
    String idNumber;//身份证号码
    Scanner scan = new Scanner(System.in);

    public BankAccount() {
    }

    public BankAccount(String id, String password) {
        Id = id;
        this.password = password;
    }

    public BankAccount(String id, String password, double money) {
        Id = id;
        this.password = password;
        this.money = money;
    }

    public BankAccount(String id, String password, double money, String phoneNumber, String name,
                       String idNumber) {
        Id = id;
        this.password = password;
        this.money = money;
        this.phoneNumber = phoneNumber;
        this.name = name;
        this.idNumber = idNumber;
    }

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdNumber() {
        return idNumber;
    }

    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }

    @Override
    public String toString() {
        return "BankAccount{" +
                "Id='" + Id + '\'' +
                ", password='" + password + '\'' +
                ", money=" + money +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", name='" + name + '\'' +
                ", idNumber='" + idNumber + '\'' +
                '}';
    }

    static ArrayList<BankAccount> list = new ArrayList<BankAccount>();

   static  {
        list.add(new BankAccount("1", "123456", 5000.0,"13696894110","李询","410221198562648"));
        list.add(new BankAccount("2", "654321", 7000.0,"12598462510","罗晨","410268945236223"));
        list.add(new BankAccount("3", "123654", 5000.0,"36891316546","宝贝","236542518986658"));
    }

    //基础存取钱操作
    //存款
    private void inputMoney(BankAccount account) {
        System.out.println("请输入您的存入金额：");
        double money = scan.nextDouble();
        if (money > 0) {
            account.setMoney(account.getMoney() + money);
            System.out.println("存款成功，本次存款金额为：" + money + "余额为:" + account.getMoney());
        } else {
            System.out.println("请输入正确金额");
        }
    }

    //取款 输入需要取出金额，账户减少相应金额
    private void takeMoney(BankAccount account) {
        System.out.println("请输入取款金额：");
        double money = scan.nextDouble();
        //取款金额大于0.且大于余额
        if (money > 0 && money <= account.getMoney()) {
            //修改账户金额
            account.setMoney(account.getMoney() - money);
            System.out.println("取款成功，本次取款金额为：" + money + "余额为:" + account.getMoney());
        } else if (money > account.getMoney()) {
            System.out.println("余额不足！");
        } else {
            System.out.println("请输入正确取款金额！");
        }

    }

    //银行余额查询
    private void showMoney(BankAccount account) {
        money = account.getMoney();
        System.out.println("当前账户余额为：" + money);
    }

    //进入银行界面
    public void user() {

        //输入密码次数不能超过3次,如果输入错误次数超过3次，则退出系统
        int n = 3;
        while (n != 1) {
            System.out.println("-----欢迎进入银行系统，请选择您的业务：----");
            System.out.println("注意：请未开户的用户到柜台进行开户！！！");
            System.out.println("1.登录账户");
            System.out.println("2.退出");
            System.out.println("请输入您的选择：");
            int key = scan.nextInt();
            A:
            while (true) {
                switch (key) {
                    case 1:
                        System.out.println("请输入您的账号");
                        Scanner i = new Scanner(System.in);
                        String s1 = i.next();
                        String ds1 = s1;
                        System.out.println("请输入您的密码");
                        Scanner p = new Scanner(System.in);
                        String s2 = p.next();
                        String ds2 = s2;
                        BankAccount card = null;
                        for (int j = 0; j < list.size(); j++) {
                            if (list.get(j).Id.equals(ds1) && list.get(j).password.equals(ds2)) {
                                System.out.println("登录成功");
                                card = list.get(j);
                                break;
                            } else {
                                n = n - 1;
                                if (n != 0) {
                                    System.out.println("密码输入错误！您还有" + n + "次机会");
                                    break;
                                } else {
                                    System.out.println("密码错误次数过多,自动退出系统");
                                    return;
                                }
                            }
                        }
                        if (card != null) {
                            while (true) {
                                System.out.println("***请输入您需要的业务***");
                                System.out.println("1.存钱 2.取钱  3.查询余额  4.退卡");
                                Scanner scan = new Scanner(System.in);//用Scanner实现键盘录入数据
                                int b = scan.nextInt();
                                //用switch语句完成操作的选择
                                switch (b) {
                                    case 1:
                                        inputMoney(card);
                                        continue;
                                    case 2:
                                        takeMoney(card);
                                        continue;
                                    case 3:
                                        showMoney(card);
                                        continue;
                                    case 4:
                                        System.out.println("欢迎下次使用");
                                        System.exit(0); //退出
                                        return;
                                }
                                break;
                            }
                        }
                        break;
                    case 2:
                        System.out.println("欢迎下次使用~");
                        return;
                    default:
                        System.out.println("请重新输入正确选项！");
                        break A;

                }
            }


        }

    }

}

